package com.lksoft.bz.action;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;

import com.lksoft.bz.Bz;

public class SaveAction extends SaveAsAction {
	private static final long serialVersionUID = 2545507663343255553L;
	
	/**
	 * @param bz The program
	 * @throws IOException 
	 */
	public SaveAction(Bz bz, String iconPath) throws IOException{
		super("", new ImageIcon(ImageIO.read(Bz.class.getResourceAsStream(iconPath)).getScaledInstance(32, 32, Image.SCALE_SMOOTH)));
		this.bz = bz;
		
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		putValue(MNEMONIC_KEY, KeyEvent.VK_S);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		doSave();
	}
	
	/**
	 * Save file to current file, or ask user if no current file
	 * @return true-If user saved  false-If user canceled
	 */
	public boolean doSave(){
		if( bz.getCurrentFile() == null ) return showSaveDialog();
		else return save(bz.getCurrentFile());
	}
}
